package com.lmscn.lms.api.interceptor;

import com.jfinal.aop.Invocation;
import com.jfinal.aop.PrototypeInterceptor;
import com.lmscn.lms.api.controller.CourseStudentController;

import java.util.List;

/**
 * Created by Czl on 2015/8/6.
 */
public class MyCourseCondition extends PrototypeInterceptor {

    private Long studentId;
    private int isPassed;
    private String isRequired;

    private CourseStudentController csc;

    /**
     * 对Condition的属性进行赋值初始化
     */
    private void init(Invocation ai) {
        csc = (CourseStudentController)ai.getController();
        studentId = csc.getParaToLong("studentId");
        isPassed = csc.getParaToInt("is_finished");
        isRequired = csc.getPara("is_required");
    }

    @Override
    public void doIntercept(Invocation inv) {
        init(inv);
        csc.myCourseCondition = this;
        inv.invoke();
    }
    /**
     * 生成CourseStudentController的查询条件
     */
    public void buildCscSql(StringBuilder sql, List<Object> paras) {
        sql.append(" from t_course_student t where 1=1 ");
        sql.append(" and student_id = ? ");
        paras.add(studentId);
        sql.append(" and course_type = ? ");
        paras.add(isRequired);
        sql.append(" and is_passed = ? ");
        paras.add(isPassed);
        // 完成的课程按照完成时间来进行降序；未完成的按照创建时间降序；2017年3月15日10:27:28
        if (isPassed == 0) {
        	sql.append(" ORDER BY create_time DESC ");
		} else {
			sql.append(" ORDER BY finished_time DESC ");
		}
    }
}
